import { ref, reactive, computed } from 'vue'
import { defineStore } from 'pinia'
import { httpGetContentByKey } from '@/api/course'

const useCourse = defineStore('course', () => {
  const courseInfo = reactive({
    id: 0,
    course_name: '',
    mind_map_id: 0,
    file_url: '',
    duration: 0,
    status: 1,
    task_id: '',
    created_at: '',
    file_keys: {},
  })
  const keyWords = ref('')
  const showSearchInput = ref(false)
  const refAutoChapters = ref(null)
  const videodomRef = ref(null)
  const wordsRef = ref(null)
  const pptExtractionRef = ref(null)
  const audioSegments = ref(null)
  const summarizationRef = reactive({
    ParagraphSummary: '',
    ParagraphTitle: '',
    ConversationalSummary: [],
    QuestionsAnsweringSummary: [],
    MindMapSummary: [],
  })
  const viewType = ref(0) // 1:双语显示 2:纯译文显示

  const content = ref('') // 笔记
  const pdfURL = ref('') // pdf地址

  /**
   * @param {Object} info
   */
  const setCourseInfo = (info) => {
    for (let k in info) {
      courseInfo[k] = info[k]
    }
    keyWords.value = ''
    showSearchInput.value = false
    wordsRef.value = null
    refAutoChapters.value = null
    summarizationRef.ParagraphSummary = ''
    pptExtractionRef.value = null
    audioSegments.value = null
  }

  /**
   * @param {boolean} data
   */
  const setShowSearchInput = (data) => {
    showSearchInput.value = data
  }

  const queryKeyFile = async (key) => {
    /** @var {Array | Object | null} response **/
    const response = await httpGetContentByKey({
      task_id: courseInfo.task_id,
      file_key: key,
      id: courseInfo.id,
    }).catch(() => null)
    return response ? response : []
  }

  /**
   *
   * @param {string} word
   */
  const setKeyWords = (word) => {
    keyWords.value = word
  }

  const words = computed(() => {
    if (wordsRef.value === null && courseInfo.id) {
      queryKeyFile('MeetingAssistance').then((res) => {
        if (res) {
          const { Keywords } = res
          wordsRef.value = Keywords
        }
      })
    }
    return wordsRef.value
  })

  const autoChapters = computed(() => {
    if (refAutoChapters.value === null && courseInfo.id) {
      queryKeyFile('AutoChapters').then((res) => {
        if (res) {
          refAutoChapters.value = res
        }
      })
    }
    return refAutoChapters.value
  })

  const summarization = computed(() => {
    if (!summarizationRef.ParagraphSummary && courseInfo.id) {
      queryKeyFile('Summarization').then((res) => {
        if (res) {
          for (let k in res) {
            summarizationRef[k] = res[k]
          }
        }
      })
    }
    return { ...summarizationRef }
  })

  const pptExtraction = computed(() => {
    if (pptExtractionRef.value === null && courseInfo.id) {
      queryKeyFile('PptExtraction').then((res) => {
        if (res) {
          const { KeyFrameList, PdfPath } = res
          pdfURL.value = PdfPath
          pptExtractionRef.value = KeyFrameList ? KeyFrameList : []
        }
      })
    }
    return pptExtractionRef.value
  })

  const setAudioSegments = (data) => {
    audioSegments.value = data
  }

  const setViewType = (type) => {
    viewType.value = type
  }
  return {
    courseInfo,
    showSearchInput,
    pptExtraction,
    words,
    keyWords,
    autoChapters,
    videodomRef,
    summarization,
    audioSegments,
    viewType,
    content,
    pdfURL,
    setKeyWords,
    setCourseInfo,
    setShowSearchInput,
    setAudioSegments,
    setViewType,
  }
})

export default useCourse
